﻿#include "MonkeyCollision.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"

using namespace cocos2d;
using namespace cocos2d::extension;

MonkeyCollision::MonkeyCollision(Monkey* monkey,int ob)
{
	setName("MonkeyCollision");
	mIsDell = kMKCNotDell;
	mIsMonkeysEat = kMKCNotMokeysEat;
	mIsBreak = kMKCNotBreak;
	mIsEnemyColision = kMKCNotEnemyCollision;
	mIsOver = kMKCIsOver;
	mMonkey = monkey;
	mObject = ob;
}

void MonkeyCollision::fPreLoadMusic()
{
	CCLog("PreLoad music !!!!");
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}

void MonkeyCollision::update(float delta)
{	
	switch(mObject)
	{
	case kFruit:
		fCollisionFruit();
		break;
	case kBough:
		fCollisionBough();
		break;
	case kEnemy:
		fCollisionEnemy();
		break;
	}

	
}

void MonkeyCollision::fCollisionFruit()
{
	if(mIsDell == kMKCIsDell || mIsMonkeysEat == kMKCIsMokeysEat)
	{
		return;
	}
	mFruit = (Fruit *)getOwner();
	if(mFruit==NULL)
	{
		return;
	}
	CCRect nBDBFruit = mFruit->boundingBox();
	CCPoint nNewPointFruit = mFruit->getParent()->convertToWorldSpace(mFruit->getPosition());
	CCRect * nNewRectFruit =new CCRect(nNewPointFruit.x, nNewPointFruit.y, nBDBFruit.size.width, nBDBFruit.size.height);
	if(fIsCollision( nNewRectFruit, mMonkey->fGetArmature()))
	{
		CCLOG("tang mau");

		mIsMonkeysEat = kMKCIsMokeysEat;

		mIsDell = kMKCIsDell;

		mMonkey->fIncreaseHeath(mFruit->fGetType());
		mFruit->removeComponent("MonkeyCollision");
		mFruit->getParent()->removeChild(mFruit, true);
		//free(mFruit);
		//free(this);
	}
	if(nNewRectFruit->getMaxY() < 0)
	{
		mIsDell = kMKCIsDell;
		mFruit->removeComponent("MonkeyCollision");
		mFruit->getParent()->removeChild(mFruit, true);
		//free(mFruit);
		//free(this);
	}
}

void MonkeyCollision::fCollisionBough()
{
	if(mIsDell == kMKCIsDell)
	{
		return;
	}
	mBough = (Bough *)getOwner();
	if(mBough==NULL)
	{
		return;
	}
	CCRect nBDBBough = mBough->boundingBox();
	CCPoint nNewPointBough;
	if(mBough->getParent()!=NULL)
	{
		nNewPointBough = mBough->getParent()->convertToWorldSpace(mBough->getPosition());
	}
	else{
		nNewPointBough = ccp(0,0);
	}
	CCRect * nNewRectBough =new CCRect(nNewPointBough.x, nNewPointBough.y, nBDBBough.size.width, nBDBBough.size.height);
	if(fIsCollision( nNewRectBough, mMonkey->fGetArmature()))
	{
		CCLOG("va cham voi cay");
		mIsBreak = kMKCIsBreak;
	}
	if(fIsOver( nNewRectBough, mMonkey->fGetArmature()) && mIsBreak == kMKCNotBreak && mIsOver == kMKCNotOver)
	{
		CCLOG("khi vuot qua canh cay");
		CCLOG("tang diem");
		((HelloWorld*)mBough->getParent()->getParent())->fSetScore(((HelloWorld*)mBough->getParent()->getParent())->fGetScore() +1);
		mIsOver = kMKCIsOver;
	}
	if(nNewRectBough->getMaxY() < 0)
	{
		mIsDell = kMKCIsDell;
		mBough->removeComponent("MonkeyCollision");
		mBough->getParent()->removeChild(mBough, true);
		//free(mBough);
		//free(this);
	}
}

void MonkeyCollision::fCollisionEnemy()
{
	if(mIsDell == 1)
	{
		return;
	}
	mEnemy = (Enemy *)getOwner();
	if(mEnemy==NULL)
	{
		return;
	}
	CCRect nBDBEnemy = mEnemy->boundingBox();
	CCPoint nNewPointEnemy = mEnemy->getParent()->convertToWorldSpace(mEnemy->getPosition());
	CCRect * nNewRectEnemy =new CCRect(nNewPointEnemy.x, nNewPointEnemy.y, nBDBEnemy.size.width, nBDBEnemy.size.height);
	if(fIsCollision( nNewRectEnemy, mMonkey->fGetArmature()))
	{
		CCLOG("dung ke thu");
		mIsEnemyColision = kMKCIsEnemyCollision;
	}
	if(fIsOver( nNewRectEnemy, mMonkey->fGetArmature()) && mIsEnemyColision == kMKCNotEnemyCollision && mIsOver == kMKCNotOver)
	{
		CCLOG("khi vuot qua canh cay");
		CCLOG("tang diem");
		((HelloWorld*)mEnemy->getParent()->getParent())->fSetScore(((HelloWorld*)mBough->getParent()->getParent())->fGetScore() +1);
		mIsOver = kMKCIsOver;
	}
	if(nNewRectEnemy->getMaxY() < 0)
	{
		mIsDell = kMKCIsDell;
		mEnemy->removeComponent("MonkeyCollision");
		mEnemy->getParent()->removeChild(mEnemy, true);
		//free(mEnemy);
		//free(this);
	}
}

bool MonkeyCollision::fIsCollision(CCRect* sp1, CCArmature* sp2)
{
	CCRect* rectSp1 = sp1;
	CCRect rectSp2 = sp2->getBone("Layer6")->getDisplayRenderNode()->boundingBox();
	float minX1, minY1, maxX1, maxY1, minX2, minY2, maxX2, maxY2;
	float nScaleMonkey = sp2->getScaleX();
	CCPoint nPointMonkey = mMonkey->getPosition();

	//set tham so object 1
	minX1 = rectSp1->getMinX();
	minY1 = rectSp1->getMinY();
	maxX1 = rectSp1->getMaxX();
	maxY1 = rectSp1->getMaxY();

	//set tham so object 2
	minX2 = rectSp2.getMinX() * nScaleMonkey + nPointMonkey.x;
	minY2 = rectSp2.getMinY() + nPointMonkey.y;
	maxX2 = rectSp2.getMaxX() * nScaleMonkey + nPointMonkey.x;
	maxY2 = rectSp2.getMaxY() + nPointMonkey.y;

	return !(maxX1 < minX2 ||
			 maxX2 < minX1 ||
			 maxY1 < minY2 ||
			 maxY2 < minY1);
}

bool MonkeyCollision::fIsOver(CCRect* sp1, CCArmature* sp2)
{
	float minX1, minY1, maxX1, maxY1, minX2, minY2, maxX2, maxY2;

	CCRect* rectSp1 = sp1;
	CCRect rectSp2 = sp2->boundingBox();
	
	//set tham so object 1 lay maxy1
	maxY1 = rectSp1->getMaxY();

	//set tham so object 2 min y2
	minY2 = rectSp2.getMinY();

	return minY2 > maxY1;
}

void MonkeyCollision::onEnter()
{
	
}
